package com.iteaj.iot.test.client.udp;

import com.iteaj.iot.client.udp.UdpClientConnectProperties;
import com.iteaj.iot.test.IotTestHandle;
import com.iteaj.iot.test.IotTestProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@ConditionalOnProperty(prefix = "iot.test", name = "udp.start", havingValue = "true")
public class UdpClientTestHandle implements IotTestHandle {

    private final IotTestProperties testProperties;

    public UdpClientTestHandle(IotTestProperties testProperties) {
        this.testProperties = testProperties;
    }

    @Override
    public void start() throws Exception {
        System.out.println("------------------------------------------------ 开始UDP协议测试 -----------------------------------------------------");

        String host = testProperties.getHost();
        IotTestProperties.TestUdpConnectConfig udp = testProperties.getUdp();
        host = udp.getHost() == null ? host : udp.getHost();
        UdpClientConnectProperties connectProperties = new UdpClientConnectProperties(host, udp.getPort());
        new UdpClientTestInitProtocol("test Udp msg".getBytes()).request(connectProperties, protocol -> {
            System.out.println(protocol);
        });

        TimeUnit.SECONDS.sleep(2);
    }

    @Override
    public int getOrder() {
        return 1000 * 2;
    }
}
